/* eslint-disable camelcase */
/* eslint-disable max-len */
/* eslint-disable no-plusplus */
let isShift= false //是否按下shift键
document.addEventListener('keydown', (e) => {
  if (e.keyCode === 16) {
    isShift = true;
  }
})
document.addEventListener('keyup', (e) => {
  if (e.keyCode === 16) {
    isShift = false;
  }
})
export default {
  data () {
    return {
      svsvAllTableContainers: null,
      allTableContainers: null,
      svIsBindScrollWheel: false,
      svSelfDefineAntTbDivCls: ''
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.svAddScrollWheelToTable()
    })
  },
  methods: {
    svRemoveScrollWheelFromTable () {
      if (this.allTableContainers && this.allTableContainers.length) {
        for (let i = 0; i < this.allTableContainers.length; i++) {
          const item = this.allTableContainers[i]
          item.removeEventListener('scroll', this.svHandleScrollCustom)
          item.removeEventListener('wheel', this.svHandleWheelCustom)
        }
        this.allTableContainers = null
        this.svsvAllTableContainers = null
        this.svIsBindScrollWheel = false
      }
    },
    svAddScrollWheelToTable () {
      if (this.svIsBindScrollWheel) return
      // 找到 fixed 元素下在相关元素（左右fixed的列） .ant-table-fixed-left/.ant-table-fixed-right 元素下的 .ant-table-body-inner
      const innerFixeds = document.querySelectorAll(this.svSelfDefineAntTbDivCls + '.ant-table-body-inner')
      // 找到原有的 table 固定区域
      const innerBody = document.querySelector(this.svSelfDefineAntTbDivCls + '.ant-table-body')
      // 给所有相关的元素再加一次
      if (innerFixeds.length) {
        this.allTableContainers = [...innerFixeds]
        if (innerBody) {
          this.allTableContainers.push(innerBody)
        }
        for (let i = 0; i < this.allTableContainers.length; i++) {
          const item = this.allTableContainers[i]
          item.addEventListener('scroll', this.svHandleScrollCustom)
          item.addEventListener('wheel', this.svHandleWheelCustom)
        }
        this.svIsBindScrollWheel = true
      }
    },
    svHandleScrollCustom (e) {
      if (isShift) {
        return
      }
      const { target } = e;
      e.preventDefault()
      // Remember last scrollTop for scroll direction detecting.
      this.svsvAllTableContainers = target.scrollTop
    },
    svHandleWheelCustom (event) {
      if (isShift) {
        return;
      }
      const refBodyTable = document.querySelector(this.svSelfDefineAntTbDivCls + '.ant-table-scroll .ant-table-body');
      const refFixedColumnsBodyLeft = document.querySelector(this.svSelfDefineAntTbDivCls + '.ant-table-fixed-left .ant-table-body-inner')
      const ref_FixedColumnsBodyRight = document.querySelector(this.svSelfDefineAntTbDivCls + '.ant-table-fixed-right .ant-table-body-inner')
      event.preventDefault()
      const wd = event.deltaY
      const { target } = event
      const bodyTable = refBodyTable
      const fixedColumnsBodyLeft = refFixedColumnsBodyLeft
      const fixedColumnsBodyRight = ref_FixedColumnsBodyRight

      let scrollTop = 0
      if (this.svsvAllTableContainers) {
        scrollTop = this.svsvAllTableContainers + wd
      } else {
        scrollTop = wd
      }
      if (fixedColumnsBodyLeft && target !== fixedColumnsBodyLeft) {
        fixedColumnsBodyLeft.scrollTop = scrollTop
      }
      if (fixedColumnsBodyRight && target !== fixedColumnsBodyRight) {
        fixedColumnsBodyRight.scrollTop = scrollTop
      }
      if (bodyTable && target !== bodyTable) {
        bodyTable.scrollTop = scrollTop
      }
    }
  },
  beforeDestroy () {
    this.svRemoveScrollWheelFromTable()
  }
}
